const { Sequelize } = require('sequelize')
const { sqlLogger } = require('../logger')
// 数据库名称，数据库账号，数据库密码，数据库参数
// 上线需要修改一下密码

const password = 'wcj8667033.'

const sequelize = new Sequelize('my_blog', 'root', password, {
  host: 'localhost',
  dialect: 'mysql',
  define: {
    charset: 'utf8mb4',
    collate: 'utf8mb4_0900_ai_ci',
    timestamps: true
  },
  logging: (msg) => {
    sqlLogger.debug(msg)
  }
})

// 验证是否连接成功
;(async () => {
  try {
    await sequelize.authenticate()
    console.log('Connection has been established successfully.')
  } catch (error) {
    console.error('Unable to connect to the database:', error)
  }
})()

module.exports = sequelize
